package com.supermap.application.service;

import com.supermap.application.dao.EnterpriseProfileDao;
import com.supermap.application.dao.MicroIdenmetaDao;
import com.supermap.application.dao.MicroIdenvlDao;
import com.supermap.application.dao.MicroTablemetaDao;
import com.supermap.application.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业档案业务层 service
 */
@Service
public class EnterpriseProfileService {


    @Autowired
    EnterpriseProfileDao enterpriseProfileDao;

    @Autowired
    MicroIdenmetaDao idenmetaDao;

    @Autowired
    MicroTablemetaDao tablemetaDao;

    @Autowired
    IndustryService industryService;

    @Autowired
    MicroIdenvlDao microIdenvlDao;

    @Value("${com.supermap.micro.table.name}")
    private String tableName;


    @Value("${com.supermap.micro.from.iden.code}")
    private String formCode;

    @Value("${com.supermap.micro.park.iden.code}")
    private String parkCode;


    /**
     * @test
     * 测试用返回前端数据
     * @return map
     */
    public Map Test(){
        Map map = new HashMap<String, Object>();
        Map<String,String> map1 = new HashMap();
        Map<String,String> map2 = new HashMap();
        map1.put("A00001","18380908X");
        map1.put("A00002","430102001001");
        map1.put("A00003","湖南兴湘建设有限公司");
        map1.put("A00004","9143000018380908X1");
        map1.put("A00005","4710");
        map1.put("A00006","C");
        map1.put("A00007","430111001008");
        map1.put("A00008","23");
        map1.put("A00009","96371");
        map1.put("A00010","96371");
        map1.put("A00011","509045");
        map1.put("A00012","138");

        map2.put("A00001","组织机构代码");
        map2.put("A00002","数据处理地");
        map2.put("A00003","单位详细名称");
        map2.put("A00004","统一社会信用代码");
        map2.put("A00005","行业代码");
        map2.put("A00006","报表类别");
        map2.put("A00007","行政区划代码");
        map2.put("A00008","从业人员期末人数");
        map2.put("A00009","营业收入(千元)");
        map2.put("A00010","主营业务收入(千元)");
        map2.put("A00011","资产总计(千元)");
        map2.put("A00012","营业税金及附加(千元)");

        map.put("code",map2);
        map.put("value",map1);
        return map;
    }


    /**
     * 根据企业名称获取企业信息
     * @param name 企业名称
     * @return
     */
    public Map getEnterpriseInfoByName(String name){
        Map map = new HashMap();
        Map map1 = getTableIdenByTableName(tableName);
        Map map2 = getMapByEnterpriseName(name);
        map.put("code",map1);
        map.put("value",map2);
        return map;
    }



    /**
     * 根据组织机构代码获取企业信息
     * @param code  组织机构代码
     * @return map
     */
    public Map getEnterpriseInfoByCode(String code){
        Map map = new HashMap();
        Map map1 = getTableIdenByTableName(tableName);
        Map map2 = getMapByEnterpriseCode(code);
        map.put("code",map1);
        map.put("value",map2);
        return map;
    }


    /**
     * 根据表名获取配置字段代码和名称
     * @param tableName 数据库表名(已配置)
     * @return map
     */
    public Map getTableIdenByTableName(String tableName){
        Map map = new HashMap();
        TMicroTablemeta tMicroTablemeta = tablemetaDao.findByTableName(tableName);
        List<TMicroIdenmeta> tMicroIdenmeta = idenmetaDao.findListByMitmid(tMicroTablemeta.getMitmid());
        //遍历字段名和释义
        for (TMicroIdenmeta object : tMicroIdenmeta){
            if (object.getIdenType() == 2) {
                map.put(object.getIdenCode(),object.getIdenName() + "(" + object.getIdenUnit() + ")");
            }else{
                map.put(object.getIdenCode(),object.getIdenName());
            }

        }
        return map;
    }

    /**
     * 根据表名模糊查询企业列表
     * @param enterpriseName  表名
     * @return list
     */
    public List getEnterpriseListByName(String enterpriseName){
        List<TEnterpriseProfile> enterpriseProfile = enterpriseProfileDao.findLikeByName("%" +enterpriseName+"%");
        return enterpriseProfile;
    }


    /**
     * 根据表名模糊查询企业列表
     * @param enterpriseName  表名
     * @return map
     */
    public Map getMapByEnterpriseName(String enterpriseName){
        TEnterpriseProfile enterpriseProfile = getEnterpriseByName(enterpriseName);
        Map map = setJsonForEnterprise(enterpriseProfile);
        return map;
    }


    /**
     * 根据企业名称获取企业数据
     * @param enterpriseName 企业详细名称
     * @return 企业对象
     */
    public TEnterpriseProfile getEnterpriseByName(String enterpriseName){
        return enterpriseProfileDao.findByName(enterpriseName);
    }

    /**
     * 根据统一社会信用代码查询企业列表
     * @param code  统一社会信用代码
     * @return map
     */
    public Map getMapByEnterpriseCode(String code){
        TEnterpriseProfile enterpriseProfile = getEnterpriseByCode(code);
        Map map = setJsonForEnterprise(enterpriseProfile);
        return map;
    }


    /**
     * 根据统一社会信用代码获取企业数据
     * @param code 统一社会信用代码
     * @return 企业对象
     */
    public TEnterpriseProfile getEnterpriseByCode(String code){
        return enterpriseProfileDao.findByUniscid(code);
    }

    /**
     *  企业信息转JOSN
     * @param enterpriseProfile 企业数据
     * @return map
     */
    public Map setJsonForEnterprise(TEnterpriseProfile enterpriseProfile){
        Map map = new HashMap();
        map.put("id",enterpriseProfile.getId());
        map.put("B00001",enterpriseProfile.getB00001());
        if (enterpriseProfile.getA00011() != null){
            map.put("A00011",industryService.getIndustryNameByCode(enterpriseProfile.getA00011()));
        }else{
            map.put("A00011","");
        }
        map.put("A00002",enterpriseProfile.getA00002());
        map.put("A00004",enterpriseProfile.getA00004());
        map.put("B101",enterpriseProfile.getB101());
        String b104 = enterpriseProfile.getB104();
        String formValue = getMicroIdencode(b104,formCode);
        map.put("B104",formValue);
        map.put("A00052",enterpriseProfile.getA00052());
        map.put("A00078",enterpriseProfile.getA00078());
        String a00079 = enterpriseProfile.getA00079();
        String parkValue = getMicroIdencode(a00079,parkCode);
        map.put("A00079",parkValue);
        map.put("A301",enterpriseProfile.getA301());
        map.put("A00302",enterpriseProfile.getA00302());
        map.put("A00303",enterpriseProfile.getA00303());
        map.put("A00075",enterpriseProfile.getA00075());
        map.put("B1921",enterpriseProfile.getB1921());
        return map;
    }

    /**
     * 根据枚举类型返回枚举值
     * @param value 枚举前值
     * @param code 枚举表miitid
     * @return 枚举后值
     */
    public String getMicroIdencode(String value,String code){
        if (value != "" || value != null){
            List<TMicroIdenvl> list = microIdenvlDao.findByTMicroIdent(Integer.valueOf(code));
            for (TMicroIdenvl tMicroIdenvl : list) {
                if (tMicroIdenvl.getCode().equals(value)){
                    return tMicroIdenvl.getName();
                }
            }
        }
        return value;
    }


}
